"use strict";

Object.defineProperty(exports, "__esModule", {
  value: true
});
exports.createParamsUrl = exports.onBeforeUpload = exports.preViewFile = exports.createUploadUrl = void 0;

require("antd/lib/message/style/css");

var _message2 = _interopRequireDefault(require("antd/lib/message"));

require("antd/lib/modal/style/css");

var _modal = _interopRequireDefault(require("antd/lib/modal"));

var _icons = require("@ant-design/icons");

var _react = _interopRequireDefault(require("react"));

var _qs = _interopRequireDefault(require("qs"));

var _localStorage = _interopRequireDefault(require("../utils/localStorage"));

var _preView = _interopRequireDefault(require("./preView"));

var _configure = require("../utils/configure");

function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }

function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); if (enumerableOnly) symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; }); keys.push.apply(keys, symbols); } return keys; }

function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = arguments[i] != null ? arguments[i] : {}; if (i % 2) { ownKeys(Object(source), true).forEach(function (key) { _defineProperty(target, key, source[key]); }); } else if (Object.getOwnPropertyDescriptors) { Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)); } else { ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } } return target; }

function _defineProperty(obj, key, value) { if (key in obj) { Object.defineProperty(obj, key, { value: value, enumerable: true, configurable: true, writable: true }); } else { obj[key] = value; } return obj; }

// 获取默认配置
var createUploadUrl = function createUploadUrl() {
  var params = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : {};
  return "".concat((0, _configure.getConfigure)().uploadApi).concat(_qs.default.stringify(_objectSpread(_objectSpread({}, params), {}, {
    Client: (0, _configure.getConfigure)().client,
    Authorization: _localStorage.default.getItem('token')
  }), {
    addQueryPrefix: true,
    allowDots: true // 多层级对象

  }));
}; // 预览文件功能


exports.createUploadUrl = createUploadUrl;

var preViewFile = function preViewFile(e) {
  var fileData = {
    fileId: e.fileId,
    fileCode: e.fileCode,
    fileName: e.fileName,
    Client: (0, _configure.getConfigure)().client,
    Authorization: _localStorage.default.getItem('token')
  };

  if (!fileData.fileName && e.name) {
    fileData.fileName = e.name;
  }

  if (!fileData.fileId && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].id;
  }

  if (!fileData.fileCode && e.response && e.response.models) {
    fileData.fileId = e.response.models[0] && e.response.models[0].code;
  }

  var downLoadURL = "".concat((0, _configure.getConfigure)().downloadApi).concat(_qs.default.stringify({
    fileId: fileData.fileId,
    fileCode: fileData.fileCode,
    Client: fileData.Client,
    Authorization: fileData.Authorization
  }, {
    addQueryPrefix: true,
    allowDots: true
  }));

  _modal.default.info({
    icon: /*#__PURE__*/_react.default.createElement(_icons.FileTextOutlined, null),
    title: fileData.fileName,
    content: /*#__PURE__*/_react.default.createElement("p", {
      style: {
        paddingTop: '10px'
      }
    }, /*#__PURE__*/_react.default.createElement(_preView.default, {
      viewUrl: downLoadURL,
      viewId: fileData.fileId,
      viewCode: fileData.fileCode,
      viewName: fileData.fileName,
      style: {
        marginRight: '10px'
      }
    }), /*#__PURE__*/_react.default.createElement("span", null, /*#__PURE__*/_react.default.createElement("a", {
      href: downLoadURL,
      onClick: function onClick() {
        _modal.default.destroyAll();
      },
      download: fileData.fileName,
      target: "_blank",
      rel: "noopener noreferrer"
    }, "\u4E0B\u8F7D"))),
    okText: '关闭',
    onOk: function onOk() {
      _modal.default.destroyAll();
    }
  });

  return true;
};
/*
 * 上传文件之前的钩子，参数为上传的文件，若返回 false 则停止上传
 * 上传前过滤一些危险文件
 * */


exports.preViewFile = preViewFile;

var onBeforeUpload = function onBeforeUpload(file, fileList) {
  var mess = arguments.length > 2 && arguments[2] !== undefined ? arguments[2] : 0;
  var arr = ['cmd', 'exe', 'bat', 'vbs', 'dll'];
  var as = file.name && file.name.split('.')[1];

  if (arr.indexOf(as) >= 0) {
    if (!mess) {
      _message2.default.error('该文件存在风险，不可上传!');
    }

    return false;
  }

  var isLt20M = file.size / 1024 / 1024 <= 50;

  if (file.size && !isLt20M) {
    if (!mess) {
      _message2.default.error('请选择小于50MB的文件!');
    }

    return false;
  }

  return true;
}; // 生成带参数链接


exports.onBeforeUpload = onBeforeUpload;

var createParamsUrl = function createParamsUrl(params, target, url) {
  var baseApi = '';

  if (target === 'download') {
    // 下载
    baseApi = (0, _configure.getConfigure)().downloadApi;
  } else if (target === 'upload') {
    // 上传
    baseApi = (0, _configure.getConfigure)().uploadApi;
  } else {
    // 自定义
    baseApi = url;
  }

  return "".concat(baseApi).concat(_qs.default.stringify(_objectSpread(_objectSpread({}, params), {}, {
    Client: (0, _configure.getConfigure)().client,
    Authorization: _localStorage.default.getItem('token')
  }), {
    addQueryPrefix: true,
    allowDots: true // 多层级对象

  }));
};

exports.createParamsUrl = createParamsUrl;